home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cuj1008.zip / RAMEY.ZIP / FIO.C < prev    next >
C/C++ Source or Header  |  1991-10-14  |  2KB  |  80 lines

  1. /*
  2. Copyright (c) Robert Ramey 1991. All Rights Reserved
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "psort.h"
  8. #include "io.h"
  9.  
  10. private
  11. MEM_SIZE buffer_size = B_SIZE;
  12.     /* size of sequencial i/o buffers  in K*/
  13. private
  14. char *io_buffer;
  15.     /* use same buffer for input and output to save space */
  16. /*********************************************************************
  17. io_init - setup io
  18. **********************************************************************/
  19. void
  20. io_init(argc, argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.     int i;
  25.  
  26.     i = arg_find(argc, argv, "-b");
  27.     if( i == -1)
  28.         i = arg_find(argc, argv, "-B");
  29.  
  30.     if(i != -1){
  31.         argv[i] = "";
  32.         arg_value(argv[++i], &buffer_size);
  33.         if(buffer_size > MAX_BUFFER_SIZE)
  34.             buffer_size = MAX_BUFFER_SIZE;
  35.         argv[i] = "";
  36.     }
  37.     io_buffer = malloc(buffer_size * K);
  38.     if(!io_buffer)
  39.         error("Couldn't get space for i/o buffers");
  40.     if(setvbuf(stdin, io_buffer, _IOFBF, buffer_size * K)
  41.     || setvbuf(stdout, io_buffer, _IOFBF, buffer_size * K))
  42.         error("Couldn't set i/o buffers");
  43.     return;
  44. }
  45. /*********************************************************************
  46. efwrite - write a record to disk and check for error
  47. **********************************************************************/
  48. void
  49. efwrite(source, size, count, fptr)
  50. char *source;
  51. unsigned int size, count;
  52. FILE *fptr;
  53. {
  54.     int i;
  55.  
  56.     i = fwrite(source, size, count, fptr);
  57.     if(i != count){
  58.         perror("Error on file write");
  59.         exit(1);
  60.     }
  61. }
  62. /*********************************************************************
  63. efread - read a record from disk and check for error
  64. **********************************************************************/
  65. int
  66. efread(dest, size, count, fptr)
  67. char *dest;
  68. unsigned int size, count;
  69. FILE *fptr;
  70. {
  71.     int i;
  72.  
  73.     i = fread(dest, size, count, fptr);
  74.     if(i != count){
  75.         perror("Error on file read");
  76.         exit(1);
  77.     }
  78.     return count;
  79. }
  80.